#include <chrono>
#include <iostream>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

class PublisherNode : public rclcpp::Node {
public:
  PublisherNode(std::string name) : Node(name) {
    publisher_ = this->create_publisher<std_msgs::msg::String>("topic_01", 10);
    publisher_->get_publisher_handle();
    timer_ = this->create_wall_timer(
        500ms, std::bind(&PublisherNode::_timer_callback, this));
  }

private:
  void _timer_callback() {
    auto msg = std_msgs::msg::String();
    msg.data = "hello world!";
    publisher_->publish(msg);
    RCLCPP_INFO(this->get_logger(), "Pulishing: '%s'", msg.data.c_str());
  }
  rclcpp::TimerBase::SharedPtr timer_{};
  rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_{};
};

int main(int argc, char **argv) {
  // 初始化ros2的C++接口
  rclcpp::init(argc, argv);
  // 建立节点对象
  auto node = std::make_shared<PublisherNode>("publisher_01");
  // 运行节点，检测退出信号
  rclcpp::spin(node);
  // 销毁节点并关闭C++接口
  rclcpp::shutdown();

  return 0;
}